package com.cloudlinkscm.base.authcenter.assembler;

import com.cloudlinkscm.base.authcenter.constant.AuthCenterService;
import com.cloudlinkscm.base.authcenter.constant.ResourceTypeEnum;
import com.cloudlinkscm.base.authcenter.model.PublicResource;
import com.cloudlinkscm.base.authcenter.model.dto.PublicResourceCreateDto;
import com.cloudlinkscm.base.authcenter.model.dto.PermissionPublicResourceCreateDto;
import com.cloudlinkscm.base.authcenter.model.dto.SubPublicResourceCreateDto;
import com.cloudlinkscm.loms.framework.core.pojo.SingleAssembler;
import org.springframework.stereotype.Component;

/**
 * @author : tac
 * @date : 31/07/2017
 */

@Component
public class PublicResourceCreateDto2PublicResourceAssembler implements SingleAssembler<PublicResourceCreateDto, PublicResource> {

    @Override
    public PublicResource assemble(PublicResourceCreateDto from) {
        PublicResource to = new PublicResource();
        to.setResourceName(from.getResourceName());
        to.setResourceEnName(from.getResourceEnName());
	    to.setResourceKey(from.getResourceKey());

        if(from instanceof SubPublicResourceCreateDto){
            to.setPid(((SubPublicResourceCreateDto) from).getPid());

            if(from instanceof PermissionPublicResourceCreateDto){
                to.setResourceKey(((PermissionPublicResourceCreateDto) from).getResourceKey());
            }
        }else{
            to.setPid(AuthCenterService.ROOT_RESOURCE_ID);
        }

        return to;
    }

    public PublicResource assemble(PublicResourceCreateDto from, ResourceTypeEnum type) {
        PublicResource to = assemble(from);
        to.setResourceType(type.code);
        return to;
    }
}
